package app.controller;

import javax.validation.Valid;   

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import app.manager.BalanceManager;
import app.manager.UserManager;
import app.model.*;

@Controller
public class UserBalanceController {
	
	private UserManager userManager;
	
	private BalanceManager balanceManager;
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String loginIn(@Valid User user,ModelMap model){
		
		model.addAttribute("login",user.getLogin());
		model.addAttribute("password",user.getPassword());
		
		return "";
	}
	
	@RequestMapping(value="/balance",method=RequestMethod.POST)
	public String updateBalance(ModelMap model,Balance balance){
		
		balanceManager.update(balance);
		
		return "login";
	}
	
	@RequestMapping(value="/admin",method=RequestMethod.GET)
	public String adminTable(ModelMap model){
		
		//UserBalance list
		return "edit_user";
	}
	
	@RequestMapping(value="/edit_user",method=RequestMethod.POST)
	public String updateUser(User user,Balance balance,ModelMap model){
		
		userManager.update(user);
		balanceManager.update(balance);
		
		return  "admin";
	}
	
	@RequestMapping(value="/registration" , method=RequestMethod.POST)
	public String createUser(@Valid @ModelAttribute User user,ModelMap model){
		
		userManager.create(user);
		
		return "balance";
	}
}
